﻿#include "appconfig.h"
#include <iostream>
#include "../Debug/DEBUG.hpp"

using namespace nlohmann;

AppConfig::AppConfig(){


}

AppConfig &AppConfig::getInstance()
{
    static AppConfig stance;
    return stance;
}

bool AppConfig::init(const std::string& setting)
{
    try {
        globalStore = json::parse(setting.c_str());
    } catch(std::exception &e) {
        DEBUG_INFO(e.what());
        return false;
    }
    return true;
}

std::string AppConfig::globalValue(const std::string &key)
{
    if (globalStore.find(key) != globalStore.end()) {
        return globalStore.at(key);
    }
    throw "globalValue无法找到key："+key;
}

std::string AppConfig::runtimeValue(const std::string &key)
{
    if (runtimeStore.find(key) != runtimeStore.end()) {
        return runtimeStore[key];
    }
    throw "runtimeValue无法找到key："+key;
}

void AppConfig::setRuntimeValue(const std::string &key, const std::string value)
{
    runtimeStore[key] = value;
}

void AppConfig::setGlobalValue(const std::string &key, const std::string value)
{
    globalStore[key] = value;
}

json AppConfig::getGlobalStore()
{
    return globalStore;
}
